<?php

class TimeDAO{
	/**
	 * 
	 * @var TimeDAO
	 */
	public static $instancia=null;
	/**
	 * 
	 * @var PDO
	 */
	private $conn;
	
	/**
	 * 
	 * @var PDOStatement
	 */
	private $stmt;
	
	protected function __construct(){
		$this->conn = ConexaoPDO::getInstancia()->getDb();
	}
	public static function getInstancia(){
		if(!isset(self::$instancia)){
			self::$instancia = new TimeDAO();
		}
		return self::$instancia;
	}
	
	public function montarObj($dados){
		$time = new Time();
		if(isset($dados["id_time"]))
			$time->setIdTime($dados["id_time"]);
		if(isset($dados["nome"]))
			$time->setNome($dados["nome"]);
		if(isset($dados["foto"]))
			$time->setFoto($dados["foto"]);
		if(isset($dados["qtd_torcedores"]))
			$time->setQtdTorcedores($dados["qtd_torcedores"]);
		if(isset($dados["pontuacao"]))
			$time->setPontuacao($dados["pontuacao"]);
		if(isset($dados["qtd_jogos"]))
			$time->setQtdJogos($dados["qtd_jogos"]);
		if(isset($dados["vitorias"]))
			$time->setVitorias($dados["vitorias"]);
		if(isset($dados["empates"]))
			$time->setEmpates($dados["empates"]);
		if(isset($dados["derrotas"]))
			$time->setDerrotas($dados["derrotas"]);
		if(isset($dados["cor1"]))
			$time->setCor1($dados["cor1"]);
		if(isset($dados["cor2"]))
			$time->setCor2($dados["cor2"]);
		if(isset($dados["foto_grande"]))
			$time->setFotoGrande($dados["foto_grande"]);
		
		return $time;
	}
	
	public function getTimes($orderBy = "pontuacao", $tipo="ASC"){
				
		$sql = "SELECT * FROM time ORDER BY $orderBy $tipo";
		try{
			$times = array();
			$this->stmt = $this->conn->query($sql,PDO::FETCH_ASSOC);
			$result = $this->stmt->fetchAll();
			foreach($result as $row){
				$times[] = $this->montarObj($row);
			}
			return $times;
		}catch(PDOException $e){
			throw new TimeException($e->getMessage());
		}
	}
	
	
	public function escolherTime($idUsuario, $idTime){
		$sql = "INSERT INTO usuario_time (id_facebook,id_time) VALUES (:id_usuario, :id_time)";
		try{
			$this->stmt = $this->conn->prepare($sql);
			$this->stmt->bindValue(":id_usuario", $idUsuario);
			$this->stmt->bindValue(":id_time", $idTime);
			$this->conn->execute($sql);
			return $this->conn->lastInsertId();
		}catch(PDOException $e){
			throw new TimeException($e->getMessage());
		}
	}
	
	public function getTime($idTime){
		$sql = "SELECT * FROM time WHERE id_time = :idTime";
		try{
			$this->stmt = $this->conn->prepare($sql);
			$this->stmt->bindValue(":idTime",$idTime);
			$this->stmt->execute();
			$result = $this->stmt->fetch();
			if(isset($result["id_time"]))
				return $this->montarObj($result);
			else
				return null;	
		}catch(PDOException $e){
			throw new TimeException($e->getMessage());
		}
	}
	
	public function getTimeByUsuario($idUsuario){
		$sql = "SELECT * FROM usuario_time WHERE id_facebook = :idUsuario";
		try{
			$this->stmt = $this->conn->prepare($sql);
			$this->stmt->bindValue(":idUsuario",$idUsuario);
			$this->stmt->execute();
			$result = $this->stmt->fetch();
			if(isset($result["id_time"]))
				return $this->getTime($result["id_time"]);
			else
				return null;	
		}catch(PDOException $e){
			throw new TimeException($e->getMessage());
		}
	}
	
	public function getTotalTorcedores(){
		
		$sql = "SELECT SUM(qtd_torcedores) as total FROM time";
		try{
			$this->stmt = $this->conn->prepare($sql);
			$this->stmt->execute();
			$result = $this->stmt->fetch();
			if(isset($result["total"]))
				return $result["total"];
			else
				return null;	
		}catch(PDOException $e){
			throw new TimeException($e->getMessage());
		}
	}
	
	
}